﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cotrobaie.UsersWebService;

namespace Cotrobaie
{
    public partial class Subscriptions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            ButtonStatistics.Attributes.Add("onMouseOut", "this.src='/images/comm.png'");
            ButtonStatistics.Attributes.Add("onMouseOver", "this.src='/images/ocomm.png'");
            ButtonDescr.Attributes.Add("onMouseOut", "this.src='/images/recc.png'");
            ButtonDescr.Attributes.Add("onMouseOver", "this.src='/images/orecc.png'");
            ButtonComments.Attributes.Add("onMouseOut", "this.src='/images/acc.png'");
            ButtonComments.Attributes.Add("onMouseOver", "this.src='/images/oacc.png'");

            //if user logged
            if (Session["UserID"] == null)
                Response.Redirect("~/Home.aspx");
            else
            {
            if (!Page.IsPostBack) Session["SubscriptionsCurrPage"] = null;

            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                var result = serviciu.GetUserAlarms((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(),0,1);
                if (result.Produse.Count() == 0)
                {
                    ButtonNextPage.Visible = false;
                    ButtonPrevPage.Visible = false;
                    Literal1.Text = "No subscriptions added yet.";
                }
                else
                {
                    PlaceHolder1.Controls.Clear();
                    Table table = new Table();
                    PlaceHolder1.Controls.Add(table);
                    // header
                    TableRow tr = new TableRow();

                    TableCell tc = new TableCell();
                    Label lab = new Label();
                    
                    
                    //actual row
                    int intrariPagina = 2;
                    if (Session["SubscriptionsCurrPage"] == null) Session["SubscriptionsCurrPage"] = 0;
                    var subscriptions = serviciu.GetUserAlarms((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), (int)Session["SubscriptionsCurrPage"], intrariPagina);

                    if (subscriptions.Produse.Count() == 0 && (int)Session["SubscriptionsCurrPage"] > 0)
                    {
                        subscriptions = serviciu.GetUserAlarms((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), (int)Session["SubscriptionsCurrPage"] - 1, intrariPagina);
                        Session["SubscriptionsCurrPage"] = (int)Session["SubscriptionsCurrPage"] - 1;
                        ButtonNextPage.Enabled = false;
                        ButtonNextPage.CssClass = "button3";
                    }
                    if (subscriptions.Produse.Count() < intrariPagina) { ButtonNextPage.Enabled = false; ButtonNextPage.CssClass = "button3"; }
                    if ((int)Session["SubscriptionsCurrPage"] == 0) { ButtonPrevPage.Enabled = false; ButtonPrevPage.CssClass = "button3"; }
                    LabelCurrentPage.Text = ((int)Session["SubscriptionsCurrPage"] + 1).ToString();
                    int i = 0;

                    foreach (var s in subscriptions.Produse)
                    {
                        i++;
                        tr = new TableRow();

                        tc = new TableCell();
                        lab = new Label();

                        tc = new TableCell();
                        HyperLink image = new HyperLink();
                        image.CssClass = "smallThumb";
                        image.Text = "<img class=\"smallThumb\" src=\"" + (s.imageLarge != null && s.imageLarge.Length > 0 ? s.imageLarge : (s.imageMedium != null && s.imageMedium.Length > 0 ? s.imageMedium : (s.imageSmall != null && s.imageSmall.Length > 0 ? s.imageSmall : "images/img.JPG"))) + "\" style=\"height:160px;\" />";
                        image.NavigateUrl = s.linkSite;

                        tc.Controls.Add(image);
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);

                        tc = new TableCell();
                        lab = new Label();
                        lab.Text = "&nbsp;";
                        tc.Controls.Add(lab);
                        tc.Width = 100;
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);

                        tc = new TableCell();
                        HyperLink hp = new HyperLink();
                        hp.NavigateUrl = s.linkSite;
                        hp.CssClass = "blgrlink";
                        hp.Text = s.name;
                        tc.Controls.Add(hp);
                        tc.Width = 180;
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);

                        //tc = new TableCell();
                        //lab = new Label();
                        //lab.Text = "$" + s.Price.ToString();
                        //tc.Controls.Add(lab);
                        //tc.Width = 90;
                        //tr.Cells.Add(tc);
                        //table.Rows.Add(tr);

                        //tc = new TableCell();
                        // lab = new Label();
                        //lab.Text = s.Price.ToString() + "%";
                        //tc.Controls.Add(lab);
                        //tc.Width = 90;
                        //tr.Cells.Add(tc);
                        //table.Rows.Add(tr);

                        tc = new TableCell();
                        lab = new Label();
                        lab.Text = "&nbsp;";
                        tc.Width = 100;
                        tc.Controls.Add(lab);
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);

                        tc = new TableCell();
                        Button but = new Button();
                        but.Text = "Change";
                        but.CssClass = "bigbutton";
                        but.Click += onClickChange;
                        but.Attributes.Add("IDProdus", s.ID.ToString());
                        but.CommandName = "change";
                        but.CommandArgument = s.ID.ToString();

                        tc.Controls.Add(but);
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);


                        //empty row
                        tr = new TableRow();
                        tc = new TableCell();
                        lab = new Label();
                        lab.Text = "&nbsp;";
                        tc.Controls.Add(lab);
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);
                    }


                }
               }
            }
        }
        private static Button Change;
        private static long ProductID;
        public void onClickChange(object sender, EventArgs e)
        {
            if (Session["UserID"] == null) { Session["LastPageVisited"] = Request.RawUrl; Response.Redirect("Login.aspx"); }

            contentBigBulina.Style.Add("top", "280px");
            alertvis.Visible = !alertvis.Visible;
            Session["SubscriptionsPageChangeButton"] = Change = (Button)sender;
            Session["SubscriptionsPageProductID"] = ProductID = long.Parse(Change.Attributes["IDProdus"].ToString());
            if (((Button)sender).Text != "Change") ((Button)sender).Text = "Change";
            else ((Button)sender).Text = "Changing...";


            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                var result = serviciu.GetUserPriceAlarm((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), ProductID);
                if (result.Success)
                {
                    var alarma = result.Alarme.First();
                    TextBoxPret.Text = alarma.AlertPrice != null ? String.Format("{0:0,0.00}", alarma.AlertPrice) : String.Empty;
                    TextBoxProcent.Text = alarma.AlertProcent != null ? String.Format("{0:0,0.00}", alarma.AlertProcent) : String.Empty;
                }
                var rez2 = serviciu.GetProdusByID(ProductID);
                if (rez2.Success) { produsText.Text = "for product <a href='" + rez2.Produse.First().linkSite + "' class='blgrlink'>" + rez2.Produse.First().name + "</a>"; }
            }
            //Page_Load(null, null);
        }
        protected void ButtonOk_Click(object sender, EventArgs e)
        {
            Change = (Button)Session["SubscriptionsPageChangeButton"];
            ProductID = (long)Session["SubscriptionsPageProductID"];
            if (TextBoxPret.Text != String.Empty)
                try
                {
                    double pret;
                    if (!double.TryParse(TextBoxPret.Text, out pret)) { ImageButtonProcAlarm.ImageUrl = "~/images/okred.png"; return; }
                    using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
                    {
                        var r = serviciu.GetProdusByID(ProductID);
                        if (r.Success)
                        { if (pret <= 0||pret>=r.Produse.First().Price) { ImageButtonProcAlarm.ImageUrl = "~/images/okred.png"; return; } }
                        else { if (pret <= 0) { ImageButtonProcAlarm.ImageUrl = "~/images/okred.png"; return; } }

                        var result = serviciu.AddAlarmPrice((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), ProductID, pret);
                        if (!result.Success) { ImageButtonProcAlarm.ImageUrl = "~/images/okred.png"; return; }
                    }
                    ImageButtonProcAlarm.ImageUrl = "~/images/okgr.png";
                }
                catch { }
            else using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
                {
                    var result = serviciu.AddAlarmPrice((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), ProductID, -1);
                    if (!result.Success) { ImageButtonProcAlarm.ImageUrl = "~/images/okred.png"; return; }
                    ImageButtonProcAlarm.ImageUrl = "~/images/ok.png";
                }
            if (TextBoxProcent.Text != String.Empty)
                try
                {
                    double pret;
                    if (!double.TryParse(TextBoxProcent.Text, out pret)) { ImageButtonPriceAlarm.ImageUrl = "~/images/okred.png"; return; }
                    if (pret <= 0 || pret >= 100) { ImageButtonPriceAlarm.ImageUrl = "~/images/okred.png"; return; }
                    using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
                    {
                        var result = serviciu.AddAlarmProcent((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), ProductID, pret);
                        if (!result.Success) { ImageButtonPriceAlarm.ImageUrl = "~/images/okred.png"; return; }
                    }
                    ImageButtonPriceAlarm.ImageUrl = "~/images/okgr.png";
                }
                catch { }
            else using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
                {
                    var result = serviciu.AddAlarmProcent((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), ProductID, -1);
                    if (!result.Success) { ImageButtonPriceAlarm.ImageUrl = "~/images/okred.png"; return; }
                    ImageButtonPriceAlarm.ImageUrl = "~/images/ok.png";
                }
            if (TextBoxPret.Text == String.Empty && TextBoxProcent.Text == String.Empty)
            {
                using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
                {
                    var result = serviciu.RemoveUserAlarm((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), ProductID);
                    if (!result.Success) { ImageButtonPriceAlarm.ImageUrl = ImageButtonProcAlarm.ImageUrl = "~/images/okred.png"; return; }
                }
                Change.ToolTip = "Not in personal alerts";
                Change.Text = "Changinged";
            }
            else
            {
                Change.ToolTip = "In alerts";
                Change.Text = "Changinged";
            }
        }
        protected void ButtonPrevPage_Click(object sender, EventArgs e)
        {
            if (Session["SubscriptionsCurrPage"] != null)
                Session["SubscriptionsCurrPage"] = (int)Session["SubscriptionsCurrPage"] - 1;
            else Session["SubscriptionsCurrPage"] = 0;
            ButtonNextPage.Enabled = true;
            ButtonNextPage.CssClass = "button2";
            Page_Load(null, null);
        }

        protected void ButtonNextPage_Click(object sender, EventArgs e)
        {
            if (Session["SubscriptionsCurrPage"] != null)
                Session["SubscriptionsCurrPage"] = (int)Session["SubscriptionsCurrPage"] + 1;
            else Session["SubscriptionsCurrPage"] = 1;
            ButtonPrevPage.Enabled = true;
            ButtonPrevPage.CssClass = "button2";
            Page_Load(null, null);
        }

        protected void goHome(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }

        public void tabClick(object sender, EventArgs e)
        {
            ImageButton but = (ImageButton)sender;

            if (but.ID == "ButtonDescr")
            {
                Response.Redirect("~/Recommendations.aspx");
            }

            else if (but.ID == "ButtonComments")
            {
                Response.Redirect("~/MyAccount.aspx");
            }

            else if (but.ID == "ButtonStatistics")
            {
                Response.Redirect("~/Comments.aspx");
            }

        }


    }
}